<?php


namespace app\api\model;


use app\common\model\BaseModel;
use common;

class CityModel extends BaseModel
{
    protected $name ='city';
    protected $pk = 'id';

    function add($info)
    {
        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'create_time' => 'string',
            'coordinate' => 'array',
            'coordinates' => 'array',
            'price_rule' => 'array',
            'admin_name' => 'string',
            'admin_phone' => 'string',
            'admin_idcno' => 'string',
            'admin_id' => 'string',
        ];
        $info = array_merge($info, [
            'create_time' => date('Y-m-d H:i:s'),
        ]);

        $info = common::type_conver($info, $selectlist);

        self::create($info,true);
        return $info;
    }

    function query_list($city_ids)
    {

        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'create_time' => 'string',
            'coordinate' => 'array',
            'price_rule' => 'array',
            'admin_name' => 'string',
            'admin_phone' => 'string',
            'admin_idcno' => 'string',
            'admin_id' => 'string',
        ];


        $esins = [];
        foreach ($city_ids as $city_id) {
            $esins[] = $city_id;
        }


        $rows = $this->whereIn('id',$esins)->order('bike_count','desc')->field(common::array_keys($selectlist))->select();
        return $rows->toArray();
    }

    function query_info($city_id)
    {
        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'create_time' => 'string',
            'coordinate' => 'array',
            'price_rule' => 'array',
            'admin_name' => 'string',
            'admin_phone' => 'string',
            'admin_idcno' => 'string',
            'admin_id' => 'string',
            'coordinates' => 'array',
            'score_rule' => 'array',
        ];

        $city_info = $this->where('id',$city_id)->find();
        if (empty($city_info)){
            return null;
        }
        else{
            return $city_info;
        }
    }

    function query_all($pos, $limit)
    {

        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'create_time' => 'string',
            'coordinate' => 'array',
            'coordinates' => 'array',
            'price_rule' => 'array',
            'admin_name' => 'string',
            'admin_phone' => 'string',
            'admin_idcno' => 'string',
            'admin_id' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
        ];


        $rows = $this->where('state','=',0)->field(common::array_keys($selectlist))->order('bike_count','desc')->limit($pos,$limit)->select();
        return $rows->toArray();
    }

    function query_all_online($pos, $limit)
    {

        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'create_time' => 'string',
            'coordinate' => 'array',
            'coordinates' => 'array',
            'price_rule' => 'array',
            'admin_name' => 'string',
            'admin_phone' => 'string',
            'admin_idcno' => 'string',
            'admin_id' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
        ];

        $where= [
            'state' => 0,
            'is_online' => 0
        ];

        $rows = $this->where($where)->field(common::array_keys($selectlist))->order('bike_count','desc')->limit($pos,$limit)->select();
        return $rows->toArray();

        return $l;
    }

    function update_city($city_id, $info)
    {
        $paramlist = [
            'name' => 'string',
            'remark' => 'string',
            'coordinate' => 'array',
            'coordinates' => 'array',
            'price_rule' => 'array',
            'admin_phone' => 'string',
            'admin_id' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);

        ApiOper::instance()->append_updater_info($data);

        $where = [
            'id' => $city_id,
        ];

        self::update($data, $where);
    }
}